var g_pageidx = 1;
$(document).ready(function(){
    if($("#morenewsbotton").length > 0){
        $("#addButton").empty();
        $("#addButton").append('<a id="next" href="load_chosen.jsp?"></a>');
    }

    $('#listContent').infinitescroll({
        navSelector   : "#next:last",
        nextSelector  : "a#next:last",
        itemSelector  : ".news_li",
        debug   : true,
        dataType    : 'html',
        maxPage : 30,
        loading: {
            msgText:'',
            //finishedMsg: '',
            img: "../img/loading.gif",
            selector: '#addButton'
        },
        finished: function(){
            if(g_pageidx >= 3){
                $("#next").remove();
                $("#infscr-loading").hide();
                $(window).unbind('.infscr');
            }
        },
        pathParse: function(path,pageidx){
            return function(pageidx){
                var lastTime = $('#last' + g_pageidx).attr("lastTime");
                g_pageidx = parseInt($('#last' + g_pageidx).attr("pageIndex"));
                g_pageidx = g_pageidx + 1;
                return "load_chosen.jsp?nodeids=25949&topCids=2313537,2313584,2313720,2308649,&pageidx=" + g_pageidx + "&lastTime=" + lastTime;
            }
        }
    },function(newElements) {
        if(g_pageidx >= 30){
            $("#addButton").css('display','block');
            $("#addButton").empty();
            $("#addButton").append('<a style="color: #01a5ec;" href="javascript:void(0);" id="morenewsbotton" onClick="morenews(this);">点击加载更多</a>');
        }

        var pageHeight = $(".main_lt").height();
        $(".main_rt").css({minHeight: pageHeight});
        shareNews('http://www.thepaper.cn');
    });

    var pageHeight = $(".main_lt").height();
    $(".main_rt").css({minHeight: pageHeight});
});


function morenews(obj){
    if(obj.disabled) return ;
    obj.disabled = true;
    var lastTime = $('#last' + g_pageidx).attr("lastTime");
    g_pageidx = parseInt($('#last' + g_pageidx).attr("pageIndex"));
    g_pageidx = g_pageidx + 1;
    $.ajax({
        type : "post",
        url : "load_chosen.jsp",
        data : "nodeids=25949&topCids=2313537,2313584,2313720,2308649,&pageidx=" + g_pageidx + "&lastTime=" + lastTime,
        timeout : 30000,
        dataType : "html",
        success : function(data) {
            data = $.trim(data);
            obj.disabled = false;
            if (data == ''){
                $("#addButton").empty();
                $("#addButton").append('<em id="loadfinished">已经加载到底部，无法提供更多数据......</em>');
                $("#loadfinished").fadeOut(3000);
            }else{
                $('#listContent').append(data);
                var pageHeight = $(".main_lt").height();
                $(".main_rt").css({minHeight: pageHeight});
                shareNews('http://www.thepaper.cn');
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            obj.disabled = false;
            $("#addButton").empty();
            $("#addButton").append('<em id="loadfinished">已经加载到底部，无法提供更多数据......</em>');
            $("#loadfinished").fadeOut(3000);
        }
    });
}

shareNews('http://www.thepaper.cn');